//
//  HNNativeHomeController.m
//  shezhan
//
//  Created by 朱传正 on 2016/10/13.
//  Copyright © 2016年 haoniu. All rights reserved.
//

#import "HNNativeHomeController.h"
#import "HNGoodsModel.h"
#import "EMSDK.h"
#import "UIImageView+EMWebCache.h"
#import "HNMainController.h"
#import "HNAllJSExport.h"
#import "WXApi.h"
//#import "WXApiRequestHandler.h"
//#import "WXApiManager.h"
//#import "SendMessageToWXReq+requestWithTextOrMediaMessage.h"
//#import "WXMediaMessage+messageConstruct.h"
#import "EMSDWebImageCompat.h"
#import "EMSDWebImageManager.h"
#import "HNMainController.h"
#import "ClassSearchController.h"
#import "HNCategoryViewController.h"
#import "iCarousel.h"
#import "HNOtherImageController.h"

#import "JPUSHService.h"

#define Kwidth  self.contentScrollView.frame.size.width

@interface HNNativeHomeController ()<UIScrollViewDelegate,UIWebViewDelegate,JPUSHRegisterDelegate>
{
         BOOL isFromStart;
}
//contentScrollView的高度
@property (nonatomic,assign)CGFloat contentHeight;

//轮播图变量
@property(nonatomic, strong)UIScrollView  *scrollView;      //声明一个UIScrollView
@property(nonatomic, strong)UIPageControl *pageControl;     //声明一个UIPageControl
@property(nonatomic, strong)NSMutableArray  *arrayImages;          //存放图片的数组
@property(nonatomic, strong)NSMutableArray *viewController; //存放UIViewController的可变数组
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,strong)UIView *rxView;

//四张图片数组
@property (nonatomic,strong)NSMutableArray *fourPicArr;

//选项卡形态控制
@property (nonatomic,strong)UILabel *choiceLabel0;
@property (nonatomic,strong)UIView *choiceLineView0;
@property (nonatomic,strong)UILabel *choiceLabel1;
@property (nonatomic,strong)UIView *choiceLineView1;
@property (nonatomic,strong)UILabel *choiceLabel2;
@property (nonatomic,strong)UIView *choiceLineView2;
@property (nonatomic,strong)UILabel *choiceLabel3;
@property (nonatomic,strong)UIView *choiceLineView3;
@property (nonatomic,strong)UILabel *choiceLabel4;
@property (nonatomic,strong)UIView *choiceLineView4;

//另一个选项卡形态控制
@property (nonatomic,strong)UILabel *choiceLabel10;
@property (nonatomic,strong)UIView *choiceLineView10;
@property (nonatomic,strong)UILabel *choiceLabel11;
@property (nonatomic,strong)UIView *choiceLineView11;
@property (nonatomic,strong)UILabel *choiceLabel12;
@property (nonatomic,strong)UIView *choiceLineView12;
@property (nonatomic,strong)UILabel *choiceLabel13;
@property (nonatomic,strong)UIView *choiceLineView13;
@property (nonatomic,strong)UILabel *choiceLabel14;
@property (nonatomic,strong)UIView *choiceLineView14;

@property (nonatomic,strong)UIView *choiceView;
@property (nonatomic,assign)CGFloat noListNormalH;//无列表的正常scrollView高度
@property (nonatomic,strong)UIView *listHeadView;
@property (nonatomic,strong)UIView *listOtherHeadView;
@property (nonatomic,assign)NSInteger choiceBtnTag;
@property (nonatomic,strong)UIView *shoppingView;//商品详情View
@property (nonatomic,assign)CGFloat *shoppingHeight;//商品详情高度
@property (nonatomic,strong)NSMutableArray *goodsDataSource;
@property (nonatomic,assign)NSInteger goodsDataCount;
@property (nonatomic,strong)HNGoodsModel *goodsModel;

@property (nonatomic,assign)NSInteger goodsPage;//商品列表页数
@property (nonatomic,strong)NSString *goodsType;//商品类型

@end

@implementation HNNativeHomeController
{//添加成员变量,因为不需要懒加载,所以不需要是属性
    iCarousel *_ic;
    UIPageControl *_pageControl;
    UILabel *_titleLb;
    NSTimer *_timer;
}


-(UIView *)listHeadView{
    if (!_listHeadView) {
        _listHeadView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, Kwidth, 36)];
   
        _listHeadView.backgroundColor = [UIColor whiteColor];
        NSArray *nameArray = [NSArray arrayWithObjects:@"推荐",@"关注",@"闲置",@"寄售",@"代购",@"筛选",nil];
        
        UIView *leftHeadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Kwidth - 70, self.listHeadView.frame.size.height)];
        
        for (int i = 0; i < nameArray.count - 1; i++) {
            UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake( leftHeadView.frame.size.width/5 * i, 0, leftHeadView.frame.size.width/5, leftHeadView.frame.size.height)];
            self.choiceView = itemView;
            [self.listHeadView addSubview:itemView];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, itemView.frame.size.width, itemView.frame.size.height - 20)];
            label.tag = 500 + i;
            label.text = nameArray[i];
            label.textColor = RGB(112, 112, 112);
            label.textAlignment =  NSTextAlignmentCenter ;
            label.font = [UIFont systemFontOfSize:14];
            [itemView addSubview:label];
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(5, itemView.frame.size.height - 1, itemView.frame.size.width-10,1)];
            lineView.tag = 500 + i;
            [lineView setHidden:YES];
            lineView.backgroundColor = RGB(229, 163, 163);
            [itemView addSubview:lineView];
            switch (i) {
                case 0:
                    self.choiceLabel0 = label;
                    self.choiceLineView0 = lineView;
                    break;
                case 1:
                    self.choiceLabel1 = label;
                    self.choiceLineView1 = lineView;
                    break;
                case 2:
                    self.choiceLabel2 = label;
                    self.choiceLineView2 = lineView;
                    break;
                case 3:
                    self.choiceLabel3 = label;
                    self.choiceLineView3 = lineView;
                    break;
                case 4:
                    self.choiceLabel4 = label;
                    self.choiceLineView4 = lineView;
                    break;
                default:
                    break;
            }
            
            //添加点击按钮 - 切换
            UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [clickBtn setTitle:@"" forState:UIControlStateNormal];
            [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
            [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            clickBtn.frame = CGRectMake(0, 0, itemView.frame.size.width, itemView.frame.size.height);//大小和位置
            clickBtn.tag = 500 + i;
            [clickBtn addTarget:self action:@selector(clickInAction:) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起后发生的事件
            [itemView addSubview:clickBtn];
            
            
        }
        
        //筛选按钮
        UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(Kwidth - 70, 0, 70, self.listHeadView.frame.size.height)];
        [self.listHeadView addSubview:rightView];
        //左边线
        UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,10, 1, rightView.frame.size.height - 20)];
        leftLabel.backgroundColor = RGB(112, 112, 112);
        [rightView  addSubview:leftLabel];
        //筛选文字
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 8, rightView .frame.size.width - 30, rightView.frame.size.height - 16)];
        label.text = nameArray[5];
        label.textColor = RGB(112, 112, 112);
        label.textAlignment =  NSTextAlignmentCenter ;
        label.textAlignment = NSTextAlignmentRight;
        label.font = [UIFont systemFontOfSize:15];
        [rightView  addSubview:label];
        //筛选图标
        UIImageView *iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(label.frame.size.width, 10, rightView.frame.size.width-label.frame.size.width - 15, rightView.frame.size.height - 20)];
        iconImageView.image = [UIImage imageNamed:@"shaixuan.png"];
        [rightView addSubview:iconImageView];
        
        //添加点击按钮 - 切换
        UIButton *filterBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [filterBtn setTitle:@"" forState:UIControlStateNormal];
        [filterBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
        [filterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        filterBtn.frame = CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height);//大小和位置
        [filterBtn addTarget:self action:@selector(shaiXuan) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起后发生的事件
        [rightView addSubview:filterBtn];
    }
    return _listHeadView;
}

-(UIView *)listOtherHeadView{
    if (!_listOtherHeadView) {
        _listOtherHeadView = [[UIView alloc]initWithFrame:CGRectMake(0, 58, Kwidth, 36)];
        
        _listOtherHeadView.backgroundColor = [UIColor whiteColor];
        NSArray *nameArray = [NSArray arrayWithObjects:@"推荐",@"关注",@"闲置",@"寄卖",@"代购",@"筛选",nil];
        
        UIView *leftHeadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Kwidth - 70, self.listOtherHeadView.frame.size.height)];
        
        for (int i = 0; i < nameArray.count - 1; i++) {
            UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake( leftHeadView.frame.size.width/5 * i, 0, leftHeadView.frame.size.width/5, leftHeadView.frame.size.height)];
            self.choiceView = itemView;
            [self.listOtherHeadView addSubview:itemView];
            
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, itemView.frame.size.width, itemView.frame.size.height - 20)];
            label.tag = 500 + i;
            label.text = nameArray[i];
            label.textColor = RGB(112, 112, 112);
            label.textAlignment =  NSTextAlignmentCenter ;
            label.font = [UIFont systemFontOfSize:14];
            [itemView addSubview:label];
            UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(5, itemView.frame.size.height - 1, itemView.frame.size.width-10,1)];
            lineView.tag = 500 + i;
            [lineView setHidden:YES];
            lineView.backgroundColor = RGB(229, 163, 163);
            [itemView addSubview:lineView];
            switch (i) {
                case 0:
                    self.choiceLabel10 = label;
                    self.choiceLineView10 = lineView;
                    break;
                case 1:
                    self.choiceLabel11 = label;
                    self.choiceLineView11 = lineView;
                    break;
                case 2:
                    self.choiceLabel12 = label;
                    self.choiceLineView12 = lineView;
                    break;
                case 3:
                    self.choiceLabel13 = label;
                    self.choiceLineView13 = lineView;
                    break;
                case 4:
                    self.choiceLabel14 = label;
                    self.choiceLineView14 = lineView;
                    break;
                default:
                    break;
            }
            
            //添加点击按钮 - 切换
            UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            [clickBtn setTitle:@"" forState:UIControlStateNormal];
            [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
            [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            clickBtn.frame = CGRectMake(0, 0, itemView.frame.size.width, itemView.frame.size.height);//大小和位置
            clickBtn.tag = 500 + i;
            [clickBtn addTarget:self action:@selector(clickInAction:) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起后发生的事件
            [itemView addSubview:clickBtn];
            
            
        }
        
        //筛选按钮
        UIView *rightView = [[UIView alloc]initWithFrame:CGRectMake(Kwidth - 70, 0, 70, self.listHeadView.frame.size.height)];
        [self.listOtherHeadView addSubview:rightView];
        //左边线
        UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,10, 1, rightView.frame.size.height - 20)];
        leftLabel.backgroundColor = RGB(112, 112, 112);
        [rightView  addSubview:leftLabel];
        //筛选文字
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 8, rightView .frame.size.width - 30, rightView.frame.size.height - 16)];
        label.text = nameArray[5];
        label.textColor = RGB(112, 112, 112);
        label.textAlignment =  NSTextAlignmentCenter ;
        label.textAlignment = NSTextAlignmentRight;
        label.font = [UIFont systemFontOfSize:15];
        [rightView  addSubview:label];
        //筛选图标
        UIImageView *iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(label.frame.size.width, 10, rightView.frame.size.width-label.frame.size.width - 15, rightView.frame.size.height - 20)];
        iconImageView.image = [UIImage imageNamed:@"shaixuan.png"];
        [rightView addSubview:iconImageView];
        
        //添加点击按钮 - 切换
        UIButton *filterBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [filterBtn setTitle:@"" forState:UIControlStateNormal];
        [filterBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
        [filterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        filterBtn.frame = CGRectMake(0, 0, rightView.frame.size.width, rightView.frame.size.height);//大小和位置
        [filterBtn addTarget:self action:@selector(shaiXuan) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起后发生的事件
        [rightView addSubview:filterBtn];
        
    }
    return _listOtherHeadView;
}


-(void)viewWillAppear:(BOOL)animated{
    [self.navigationController.navigationBar setHidden:NO];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.searchBackground.layer setCornerRadius:5];
    self.searchTextView.adjustsFontSizeToFitWidth = YES;
    self.contentScrollView.frame = CGRectMake(0, 0, mainScreenSize.width, mainScreenSize.height - 50);
//    _contentScrollView.backgroundColor = RGB(244, 244, 244);
     _contentScrollView.backgroundColor = [UIColor whiteColor];
    _contentScrollView.delegate = self;
    _contentHeight = 0;
    self.goodsDataSource = [[NSMutableArray alloc]init];
    _goodsDataCount = 0;
    _goodsPage = 0;
    _goodsType = @"0";
    
    //上拉刷新
    MJRefreshGifHeader *header = [[MJRefreshGifHeader alloc]init];
    [header setRefreshingBlock:^(void){
        [self refreshHeadDataSource];

    }];
    _contentScrollView.mj_header = header;
    //下拉刷新
    _contentScrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^(void){
        _goodsPage ++;
        [self refreshGoodsDate];
    }];
    
    //设置极光推送
    [self addTag];

    //设置广告栏
    [self setAdsVC:nil];
    
    //设置分类
    [self setCategory];
    
    //设置商品
//    [self setGoodsViewAndImages:nil];
    
    [self setGoodsListViewAndImages:nil];
    //设置列表
//    [self setListView];
    //刷新列表数据
//    [self getGoodsDate:@"0"];
    
 
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
           //初始化数据
            [self getIntializeDate];
    });

    
}


-(void)addTag{

     NSString * loginName = [NSString stringWithFormat:@"%@",[[EMClient sharedClient] currentUsername]];
//    NSMutableSet *set  = [[NSMutableSet alloc]initWithObjects:loginName, nil];
//    NSLog(@"----set = %@",set);
    
    [AppConfig setTags:nil andAlias:loginName];
    
}

-(void)setAdsVC:(NSMutableArray *)imageArr{
    if (!imageArr) {
         self.arrayImages = [NSMutableArray arrayWithObjects:@"ad_0.png", nil];
        return;
    }else{
        self.arrayImages = imageArr;
    }
    
    self.adsView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, Kwidth*3/5)];
    _contentHeight = _contentHeight + self.adsView.frame.size.height;
    _contentScrollView.contentSize = CGSizeMake(mainScreenSize.width,_contentHeight);
    [self.contentScrollView addSubview:self.adsView];
    CGFloat height = self.adsView.frame.size.height;
    CGFloat width = self.adsView.frame.size.width;
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, width,height)];
    [_scrollView setPagingEnabled:YES];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    [_scrollView setDelegate:self];
    [_scrollView setBackgroundColor:[UIColor lightGrayColor]];
    
    //ContentSize 这个属性对于UIScrollView挺关键的，取决于是否滚动。
    [_scrollView setContentSize:CGSizeMake(width * [self.arrayImages count], height)];
    [self.adsView addSubview:self.scrollView];
    
    [self.scrollView setBounces:NO];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width/2-80, self.scrollView.frame.size.height - 20, 160, 20)];
    //    [_pageControl setBackgroundColor:[UIColor blackColor]];
    
    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = [self.arrayImages count];
    [_pageControl addTarget:self action:@selector(chagePage:) forControlEvents:UIControlEventValueChanged];
    [self.adsView addSubview:self.pageControl];
    
    _viewController = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 0; i < [self.arrayImages count]; i++) {
        [_viewController addObject:[NSNull null]];
    }
    
    //加载每张图
    for (NSInteger i = 0; i<self.arrayImages.count; i++) {
        [self loadScrollViewPage:i];
    }
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:self.arrayImages.count*2 target:self selector:@selector(scrollPages) userInfo:nil repeats:YES];
  
}

-(void)setCategory{
    self.categoryView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, Kwidth, Kwidth/4)];
    _contentHeight = _contentHeight + self.categoryView.frame.size.height;
       _contentScrollView.contentSize = CGSizeMake(mainScreenSize.width, _contentHeight);
    [self.contentScrollView addSubview:self.categoryView];
    NSArray *imageArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"woyaojianding.png"], [UIImage imageNamed:@"jimai.png.png"],[UIImage imageNamed:@"weixiu.png.png"],[UIImage imageNamed:@"xianxia.png.png"],nil];
    
    NSArray *nameArray = [NSArray arrayWithObjects:@"我要鉴定",@"我要出售",@"维修养护",@"线下门店", nil];
    self.categoryView.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < imageArray.count; i++) {
        UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake( self.categoryView.frame.size.width/4 * i, 0, self.categoryView.frame.size.width/4, self.categoryView.frame.size.width/4)];
        [self.categoryView addSubview:itemView];
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 6, itemView.frame.size.width-30, itemView.frame.size.width-30)];
        imageView.image = imageArray[i];
        [itemView addSubview:imageView];
        
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, itemView.frame.size.width-22, itemView.frame.size.width, 17)];
        label.text = nameArray[i];
        label.textColor = RGB(112, 112, 112);
        label.textAlignment =  NSTextAlignmentCenter ;
        
        label.font = [UIFont systemFontOfSize:12];
        [itemView addSubview:label];
        
        //添加点击按钮 - 切换
        UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [clickBtn setTitle:@"" forState:UIControlStateNormal];
        [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
        [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        clickBtn.frame = CGRectMake(0, 0, itemView.frame.size.width, itemView.frame.size.height);//大小和位置
        clickBtn.tag = 500 + i;
        [clickBtn addTarget:self action:@selector(categoryInAction:) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起后发生的事件
        [itemView addSubview:clickBtn];
    }

}

//-(void)setGoodsViewAndImages:(NSMutableArray *)imageArr{
//    if (!imageArr) {
//        imageArr = [NSMutableArray arrayWithObjects:@"pic-2.png.png",@"pic-3.png.png",@"pic-4.png.png",@"pic-4.png.png", nil];
//    }
//    self.fourPicArr = imageArr;
//    CGFloat bl = 6;//border边距
//    CGFloat gl = 3;//gap间隙
//     _contentHeight = _contentHeight + 10;
//    self.goodsView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, Kwidth, Kwidth/2 + 6)];
//    _contentHeight = _contentHeight + self.goodsView.frame.size.height;
//    _contentScrollView.contentSize = CGSizeMake(self.view.frame.size.width, _contentHeight);
//    [self.contentScrollView addSubview:self.goodsView];
//    self.goodsView.backgroundColor = [UIColor whiteColor];
//    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(bl, bl, (self.goodsView.frame.size.width/2 - bl)*5/6, self.goodsView.frame.size.width/2 - bl)];
//     [imageView1 sd_setImageWithURL:[NSURL URLWithString:self.fourPicArr[0]] placeholderImage:[UIImage imageNamed:@"pic-2.png.png"]];
////    imageView1.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageArr[0]]]];
//    [self.goodsView addSubview:imageView1];
//    //添加点击按钮1 - 切换
//    UIButton *clickBtn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [clickBtn1 setTitle:@"" forState:UIControlStateNormal];
//    [clickBtn1 setBackgroundColor:RGBA(255, 255, 255, 0)];
//    [clickBtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    clickBtn1.frame =  imageView1.frame;//大小和位置
//    clickBtn1.tag = 500 + 11;
//    [clickBtn1 addTarget:self action:@selector(enterGoodsList:) forControlEvents:UIControlEventTouchUpInside];
//    [self.goodsView addSubview:clickBtn1];
//    UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(bl + imageView1.frame.size.width + gl, bl, Kwidth - (bl + imageView1.frame.size.width + gl) - bl, (Kwidth - (bl + imageView1.frame.size.width + gl) - bl)*25/74)];
//    [imageView2 sd_setImageWithURL:[NSURL URLWithString:self.fourPicArr[1]] placeholderImage:[UIImage imageNamed:@"pic-3.png.png"]];
////    imageView2.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageArr[1]]]];
//    [self.goodsView addSubview:imageView2];
//    //添加点击按钮2 - 切换
//    UIButton *clickBtn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [clickBtn2 setTitle:@"" forState:UIControlStateNormal];
//    [clickBtn2 setBackgroundColor:RGBA(255, 255, 255, 0)];
//    [clickBtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    clickBtn2.frame = imageView2.frame;//大小和位置
//     clickBtn2.tag = 500 + 12;
//    [clickBtn2 addTarget:self action:@selector(enterGoodsList:) forControlEvents:UIControlEventTouchUpInside];
//    [self.goodsView addSubview:clickBtn2];
//    UIImageView *imageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(bl + imageView1.frame.size.width + gl, bl + imageView2.frame.size.height + gl , (Kwidth - (bl + imageView1.frame.size.width + gl) - bl - gl)/2, _goodsView.frame.size.height - 2*bl - gl - imageView2.frame.size.height)];
//    [imageView3 sd_setImageWithURL:[NSURL URLWithString:self.fourPicArr[2]] placeholderImage:[UIImage imageNamed:@"pic-4.png.png"]];
////    imageView3.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageArr[2]]]];
//    [self.goodsView addSubview:imageView3];
//    //添加点击按钮3 - 切换
//    UIButton *clickBtn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [clickBtn3 setTitle:@"" forState:UIControlStateNormal];
//    [clickBtn3 setBackgroundColor:RGBA(255, 255, 255, 0)];
//    [clickBtn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    clickBtn3.frame =  imageView3.frame;;//大小和位置
//     clickBtn3.tag = 500 + 13;
//    [clickBtn3 addTarget:self action:@selector(enterGoodsList:) forControlEvents:UIControlEventTouchUpInside];
//    [self.goodsView addSubview:clickBtn3];
//    UIImageView *imageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(bl + imageView1.frame.size.width + gl + imageView3.frame.size.width + gl, bl + imageView2.frame.size.height + gl , (Kwidth - (bl + imageView1.frame.size.width + gl) - bl - gl)/2, _goodsView.frame.size.height - 2*bl - gl - imageView2.frame.size.height)];
//    [imageView4 sd_setImageWithURL:[NSURL URLWithString:self.fourPicArr[3]] placeholderImage:[UIImage imageNamed:@"pic-4.png.png"]];
////    imageView4.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageArr[3]]]];
//    [self.goodsView addSubview:imageView4];
//    //添加点击按钮4 - 切换
//    UIButton *clickBtn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    [clickBtn4 setTitle:@"" forState:UIControlStateNormal];
//    [clickBtn4 setBackgroundColor:RGBA(255, 255, 255, 0)];
//    [clickBtn4 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    clickBtn4.frame = imageView4.frame;//大小和位置
//     clickBtn4.tag = 500 + 14;
//    [clickBtn4 addTarget:self action:@selector(enterGoodsList:) forControlEvents:UIControlEventTouchUpInside];
//    [self.goodsView addSubview:clickBtn4];
//}

-(void)setGoodsListViewAndImages:(NSArray *)adsArr{
//    NSInteger num = adsArr.count;
    NSInteger num = 4;
    CGFloat gl = 5;
    self.goodsListView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, Kwidth,0)];
    self.goodsListView.backgroundColor = [UIColor whiteColor];
    [self.contentScrollView addSubview:self.goodsListView];
    //商品高度
    CGFloat goodsListHeight = 0;
    UIView *listHeadView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, gl)];
    goodsListHeight = goodsListHeight + listHeadView.frame.size.height;
    listHeadView.backgroundColor = [UIColor whiteColor];
    [self.goodsListView addSubview:listHeadView];
    
    CGFloat goodsHeight = (Kwidth - 2*gl)*2/5;
    
    for (NSInteger x = 0; x < num; x ++) {
//        NSDictionary *goodsDic = adsArr[x];
        
        //商品图
        UIImageView *goodsImageView = [[UIImageView alloc]initWithFrame:CGRectMake( gl,2*gl + (goodsHeight + gl) * x, Kwidth - 2*gl, goodsHeight)];
        
        NSString *goodsImageStr = [NSString stringWithFormat:@"%@",adsArr[x]];
        [goodsImageView sd_setImageWithURL:[NSURL URLWithString:goodsImageStr] placeholderImage:[UIImage imageNamed:@"gooodsList_no.png"]];
        
        [self.goodsListView addSubview:goodsImageView];
        
        //图片下面的标签
        UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake( Kwidth/2 - 60, goodsImageView.frame.size.height - 30, 120, 30)];
        bottomView.backgroundColor = RGBA(97, 5, 8, 0.7);
        [goodsImageView addSubview:bottomView];
        
//        UILabel *bottomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, bottomView.frame.size.height/2 - 9, bottomView.frame.size.width, 18)];
//        bottomLabel.text = @"专题推荐";
//        bottomLabel.textColor = RGB(254, 254, 254);
//        [bottomLabel setFont:[UIFont systemFontOfSize:16]];
//        [bottomLabel setTextAlignment:NSTextAlignmentCenter];
//        [bottomView addSubview:bottomLabel];
        
//        //商品介绍
//        UILabel *introduceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, goodsImageView.frame.size.height*2/5 - 22, goodsImageView.frame.size.width, 22)];
//        introduceLabel.text = @"举手投足 气质加分";
//        introduceLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(76.0)];
//        introduceLabel.textColor = RGB(255, 255, 255);
//        [introduceLabel setFont:[UIFont systemFontOfSize:20]];
//        [introduceLabel setTextAlignment:NSTextAlignmentCenter];
//        [goodsImageView addSubview:introduceLabel];
//        
//        //商品描述
//        UILabel *describeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, introduceLabel.frame.origin.y + introduceLabel.frame.size.height + gl, goodsImageView.frame.size.width, 16)];
//       describeLabel.text = @"举手投足 气质加分";
//        describeLabel.textColor = RGB(255, 255, 255);
//        [describeLabel setFont:[UIFont systemFontOfSize:14]];
//        [describeLabel setTextAlignment:NSTextAlignmentCenter];
//        [goodsImageView addSubview:describeLabel];
        
        //添加点击按钮
        UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [clickBtn setTitle:@"" forState:UIControlStateNormal];
        [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
        [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        clickBtn.tag = x + 10000;
        clickBtn.frame = goodsImageView.frame;//大小和位置
        [clickBtn addTarget:self action:@selector(enterGoodsList:) forControlEvents:UIControlEventTouchUpInside];
        [self.goodsListView addSubview:clickBtn]; 

    }
    CGRect listFrame =self.goodsListView.frame;
    listFrame.size.height = 2*gl + num*(goodsHeight + gl);
    self.goodsListView.frame = listFrame;
    _contentHeight = _contentHeight + self.goodsListView.frame.size.height;
    _contentScrollView.contentSize = CGSizeMake(mainScreenSize.width, _contentHeight);
}


//创建列表
-(void)setListView{
      _contentHeight = _contentHeight + 10;
    _contentHeight = _contentHeight + self.listHeadView.frame.size.height;
    //保存这个定高
    _noListNormalH = _contentHeight + self.listHeadView.frame.size.height;
    _contentScrollView.contentSize = CGSizeMake(mainScreenSize.width, _contentHeight);
    [self.contentScrollView addSubview:self.listHeadView];
    
    
    
    
    //添加列表view
    self.listView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, Kwidth, 0)];
    self.listView.backgroundColor = [UIColor whiteColor];
    
    [self.contentScrollView addSubview:self.listView];
   
         //把详情View添加到列表View
    self.shoppingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, 0)];
    [self.listView addSubview:self.shoppingView];
    //给tag赋值
    self.choiceBtnTag = 0;
    //初始按钮状态
    [self setStateColor:0];
    //创建列表
//    [self createListViewUI];
    
}

//创建列表UI
-(void)createListViewUI{
    CGFloat bl = 6;//border边距
    CGFloat gl = 4;//gap间隙
    NSInteger num = self.goodsDataSource.count;
    //当商品列表为空时
    if (num == 0) {
        _contentScrollView.contentSize = CGSizeMake(mainScreenSize.width, _noListNormalH + self.view.frame.size.height - 59 - 36 - 55);
    }
    for (NSInteger i = self.goodsDataCount; i < self.goodsDataSource.count; i++) {
        
        NSDictionary *goodsDataDic  = self.goodsDataSource[i];
        UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake((Kwidth/2 - bl/2) * !(i%2 == 0) + bl, self.shoppingView.frame.size.height, Kwidth/2, 0)];
        [self.shoppingView addSubview:itemView];
        //商品图
        UIImageView *goodsImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, gl * 2, Kwidth/2 - bl*3/2, Kwidth/2 - bl*3/2)];
        
        NSString *goodsImageStr = [NSString stringWithFormat:@"%@%@",url_Base_no,[goodsDataDic objectForKey:@"head"]];
         [goodsImageView sd_setImageWithURL:[NSURL URLWithString:goodsImageStr] placeholderImage:[UIImage imageNamed:@"pir01.png"]];
//        goodsImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:goodsImageStr]]];
        [itemView addSubview:goodsImageView];
        
        //商品按钮
        NSInteger isAgent = [[NSString stringWithFormat:@"%@",[goodsDataDic objectForKey:@"hnAgent"]] integerValue];
        NSInteger isTicketId = [[NSString stringWithFormat:@"%@",[goodsDataDic objectForKey:@"hnTicketId"]] integerValue];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        if (isAgent == 0) {
            btn.frame = CGRectMake(gl, goodsImageView.frame.size.height + 18, 0, 20);
            btn.backgroundColor  = [UIColor whiteColor];
            if (isTicketId>0) {
//                [btn setTitle:[NSString stringWithFormat:@"优惠券：%@",isTicketId ] forState:UIControlStateNormal];
                 [btn setTitle:@"促销" forState:UIControlStateNormal];
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                btn.backgroundColor = RGB(252, 91, 93);
                btn.frame = CGRectMake(gl, goodsImageView.frame.size.height + 18, btn.titleLabel.text.length * 15 +10, 20);//大小和位置
                btn.titleLabel.font = [UIFont systemFontOfSize:13];
                [btn.layer setMasksToBounds:YES];
                [btn.layer setCornerRadius:4];
                       [itemView addSubview:btn];
            }
        }else{
            [btn setTitle:@"代购" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.backgroundColor = RGB(252, 91, 93);
            btn.frame = CGRectMake(gl, goodsImageView.frame.size.height + 18, btn.titleLabel.text.length * 15 +10, 20);//大小和位置
            btn.titleLabel.font = [UIFont systemFontOfSize:13];
            [btn.layer setMasksToBounds:YES];
            [btn.layer setCornerRadius:4];
            if (isTicketId>0) {
                    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [btn2 setTitle:@"促销" forState:UIControlStateNormal];
                [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                btn2.backgroundColor = RGB(252, 91, 93);
                btn2.frame = CGRectMake(gl + btn.frame.size.width + gl, goodsImageView.frame.size.height + 18, btn2.titleLabel.text.length * 15 +10, 20);//大小和位置
                btn2.titleLabel.font = [UIFont systemFontOfSize:13];
                [btn2.layer setMasksToBounds:YES];
                [btn2.layer setCornerRadius:4];
                [itemView addSubview:btn2];
            }
        }
        [btn addTarget:self action:@selector(businessInAction:) forControlEvents:UIControlEventTouchUpInside];//button被按下又抬起后发生的事件
        [itemView addSubview:btn];
        //商品名
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(gl, btn.frame.origin.y + btn.frame.size.height + gl, goodsImageView.frame.size.width - 2*gl, 15)];
        nameLabel.numberOfLines = 1;
        nameLabel.font = [UIFont systemFontOfSize:13];
        nameLabel.textColor = RGB(82, 82, 82);
        nameLabel.text = [NSString stringWithFormat:@"%@",[goodsDataDic objectForKey:@"hnTitle"]];
        [itemView addSubview:nameLabel];
        //商品描述
        UILabel *detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(gl, nameLabel.frame.origin.y + nameLabel.frame.size.height + gl, goodsImageView.frame.size.width - 2*gl, 15)];
        detailLabel.numberOfLines = 1;
        detailLabel.font = [UIFont systemFontOfSize:13];
        detailLabel.textColor = RGB(112, 112, 112);
        detailLabel.text = [NSString stringWithFormat:@"%@",[goodsDataDic objectForKey:@"hnIntroduce"]];
        [itemView addSubview:detailLabel];
        //人民币符号
        UILabel *moneyIconLabel = [[UILabel alloc]initWithFrame:CGRectMake(gl, detailLabel.frame.origin.y + detailLabel.frame.size.height + gl, 12, 15)];
        moneyIconLabel.font = [UIFont systemFontOfSize:13];
        moneyIconLabel.textAlignment = NSTextAlignmentRight;
        moneyIconLabel.textColor = RGB(0, 0, 0);
        moneyIconLabel.text = [NSString stringWithFormat:@"¥"];
        [itemView addSubview:moneyIconLabel];
         //商品价格
        UILabel *priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(gl + moneyIconLabel.frame.size.width, detailLabel.frame.origin.y + detailLabel.frame.size.height + gl, goodsImageView.frame.size.width - 2*gl - moneyIconLabel.frame.size.width, 15)];
        priceLabel.numberOfLines = 0;
        priceLabel.font = [UIFont systemFontOfSize:13];
        priceLabel.textColor = RGB(0, 0, 0);
        priceLabel.text = [NSString stringWithFormat:@"%@",[goodsDataDic objectForKey:@"hnPrice"]];
        [itemView addSubview:priceLabel];
        
        //添加点击按钮
        UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [clickBtn setTitle:@"" forState:UIControlStateNormal];
        [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
        [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        clickBtn.tag = i + 10000;
        clickBtn.frame = goodsImageView.frame;//大小和位置
        [clickBtn addTarget:self action:@selector(enterGoodsDetail:) forControlEvents:UIControlEventTouchUpInside];
        [itemView addSubview:clickBtn];
        
        //设置列表高度
        CGRect itemViewFrame = itemView.frame;
        itemViewFrame.size.height = itemViewFrame.size.height + 20 + 9*gl + goodsImageView.frame.size.height + btn.frame.size.height + nameLabel.frame.size.height + priceLabel.frame.size.height;
        itemView.frame = itemViewFrame;
        if (i%2 == 1 || (i + 1 == num && num%2 == 1)) {
            //设置列表高度
            _contentHeight = _contentHeight + itemView.frame.size.height;
            //最后一行加载后再改变scrollView高度
            if(i + 1 == num){
              _contentScrollView.contentSize = CGSizeMake(mainScreenSize.width, _contentHeight);
            }
            //设置列表高度
            CGRect listViewFrame = self.listView.frame;
            listViewFrame.size.height = listViewFrame.size.height + itemView.frame.size.height;
            self.listView.frame = listViewFrame;
            //设置商品高度
            CGRect shoppingViewFrame = self.shoppingView.frame;
            shoppingViewFrame.size.height = shoppingViewFrame.size.height + itemView.frame.size.height;
            self.shoppingView.frame = shoppingViewFrame;
        }
    }
    self.goodsDataCount = self.goodsDataSource.count;
}



#pragma mark - 切换按钮 方法

-(void)categoryInAction:(UIButton *)btn{
    HNUnusedController *unuserVC = [HNUnusedController new];
    HNConsignmentController *consignmentVC = [HNConsignmentController new];
    HNRepairController *repairVC = [HNRepairController new];
    HNLineController *lineVC = [HNLineController new];

    switch (btn.tag - 500) {
        case 0://闲置
            [self.navigationController pushViewController:unuserVC animated:YES];

            break;
        case 1://寄卖速卖
            [self.navigationController pushViewController:consignmentVC animated:YES];

            break;
        case 2://维修养护
            [self.navigationController pushViewController:repairVC animated:YES];

            break;
        case 3://线下门店
            [self.navigationController pushViewController:lineVC animated:YES];

            break;
        default:
            break;
    }

}

//点击 四张图 + 关注 执行的方法
-(void)enterGoodsList:(UIButton *)btn{
    //关注btn = 500 + 1 四张图 btn = 500 + 11\12\13\14
    if (btn.tag == 501) {
          btn.selected = NO;
        //返回必须调用方法goBackRefresh
        HNConcernController *concernVC = [[HNConcernController alloc]init];
        concernVC.type = @"1";
        [self.navigationController pushViewController:concernVC animated:YES];
    }else{
        HNConcernController *concernVC = [[HNConcernController alloc]init];
        [self.navigationController pushViewController:concernVC animated:YES];
    }
  
//   [self presentViewController:concernVC animated:YES completion:nil];
}


-(void)enterGoodsDetail:(UIButton *)btn{
    NSInteger i = btn.tag - 10000;
    NSDictionary *goodsDataDic  = self.goodsDataSource[i];
    HNGoodsDetailController *detailVC = [HNGoodsDetailController new];
    detailVC.productId = [goodsDataDic objectForKey:@"hnProductId"];
        [self.navigationController pushViewController:detailVC animated:YES];
}

//推荐、闲置、寄卖、代购选择后执行的方法
-(void)clickInAction:(UIButton *)btn{
    if (self.choiceBtnTag == btn.tag - 500) {
        return;
    }else if(btn.tag - 500 == 1){
        btn.selected = NO;
        [self enterGoodsList:btn];

    }else{
        CGRect goodsFrame = self.goodsView.frame;
        goodsFrame.size.height = 0;
        self.goodsView.frame = goodsFrame;
        //删掉以前的view
        [self.shoppingView removeFromSuperview];
        //重置商品列表
        self.shoppingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, 0)];
        [self.listView addSubview:self.shoppingView];
        //重置列表高度
        //设置列表高度
        CGRect listViewFrame = self.listView.frame;
        listViewFrame.size.height = 0;
        self.listView.frame = listViewFrame;
        //切换按钮--刷新数据的同时，把scrollView拉回定值
        CGFloat offset = _noListNormalH - 58 - 36 - 36;
        CGFloat contentScrolloffectY = _contentScrollView.contentOffset.y;
        if (contentScrolloffectY>offset) {
            _contentScrollView.contentOffset = CGPointMake(_contentScrollView.contentOffset.x, offset);
        }
        //重置总高度
        _contentHeight = _noListNormalH - self.listHeadView.frame.size.height;
        //重置总数据
       [self.goodsDataSource removeAllObjects];

        _goodsDataCount = 0;
    }
    self.choiceBtnTag = btn.tag - 500;
    int i = [self.goodsType intValue];
    if (i!=0) {
        i = i + 500 + 1;
    }else{
        i = i + 500;
    }
//    UIButton *currentBtn = (UIButton *)[self.view viewWithTag:(i)];
    switch (btn.tag - 500) {
        case 0://推荐
            [self setStateColor:0];
            self.goodsType = @"0";
            [self getGoodsDate:@"0"];
            break;
        case 1://关注
//            [self setStateColor:1];
//            [self clickInAction:currentBtn];//保证会退回来有数据
            
            break;
        case 2://闲置
            [self setStateColor:2];
             self.goodsType = @"1";
            [self getGoodsDate:@"1"];
            break;
        case 3://寄卖
              [self setStateColor:3];
                 self.goodsType = @"2";
               [self getGoodsDate:@"2"];
            break;
        case 4://代购
              [self setStateColor:4];
                 self.goodsType = @"3";
               [self getGoodsDate:@"3"];
            break;
            
        default:
            break;
    }
}

//筛选
-(void)shaiXuan{
    HNFilterController *filterVC = [[HNFilterController alloc]init];
    [self.navigationController pushViewController:filterVC animated:YES];
}

//选择按钮状态变换
-(void)setStateColor:(NSInteger)num{
    switch (num) {
        case 0://推荐
            self.choiceLabel0.textColor = RGB(229, 163, 163);
            [self.choiceLineView0 setHidden:NO];
            self.choiceLabel1.textColor = RGB(112, 112, 112);
            [self.choiceLineView1 setHidden:YES];
            self.choiceLabel2.textColor = RGB(112, 112, 112);
            [self.choiceLineView2 setHidden:YES];
            self.choiceLabel3.textColor = RGB(112, 112, 112);
            [self.choiceLineView3 setHidden:YES];
            self.choiceLabel4.textColor = RGB(112, 112, 112);
            [self.choiceLineView4 setHidden:YES];
            
            self.choiceLabel10.textColor = RGB(229, 163, 163);
            [self.choiceLineView10 setHidden:NO];
            self.choiceLabel11.textColor = RGB(112, 112, 112);
            [self.choiceLineView11 setHidden:YES];
            self.choiceLabel12.textColor = RGB(112, 112, 112);
            [self.choiceLineView12 setHidden:YES];
            self.choiceLabel13.textColor = RGB(112, 112, 112);
            [self.choiceLineView13 setHidden:YES];
            self.choiceLabel14.textColor = RGB(112, 112, 112);
            [self.choiceLineView14 setHidden:YES];
            break;
        case 1://关注
            self.choiceLabel0.textColor = RGB(112, 112, 112);
            [self.choiceLineView0 setHidden:YES];
            self.choiceLabel1.textColor = RGB(229, 163, 163);
            [self.choiceLineView1 setHidden:NO];
            self.choiceLabel2.textColor = RGB(112, 112, 112);
            [self.choiceLineView2 setHidden:YES];
            self.choiceLabel3.textColor = RGB(112, 112, 112);
            [self.choiceLineView3 setHidden:YES];
            self.choiceLabel4.textColor = RGB(112, 112, 112);
            [self.choiceLineView4 setHidden:YES];
            
            self.choiceLabel10.textColor = RGB(112, 112, 112);
            [self.choiceLineView10 setHidden:YES];
            self.choiceLabel11.textColor = RGB(229, 163, 163);
            [self.choiceLineView11 setHidden:NO];
            self.choiceLabel12.textColor = RGB(112, 112, 112);
            [self.choiceLineView12 setHidden:YES];
            self.choiceLabel13.textColor = RGB(112, 112, 112);
            [self.choiceLineView13 setHidden:YES];
            self.choiceLabel14.textColor = RGB(112, 112, 112);
            [self.choiceLineView14 setHidden:YES];
            break;
        case 2://闲置
            self.choiceLabel0.textColor = RGB(112, 112, 112);
            [self.choiceLineView0 setHidden:YES];
            self.choiceLabel1.textColor = RGB(112, 112, 112);
            [self.choiceLineView1 setHidden:YES];
            self.choiceLabel2.textColor = RGB(229, 163, 163);
            [self.choiceLineView2 setHidden:NO];
            self.choiceLabel3.textColor = RGB(112, 112, 112);
            [self.choiceLineView3 setHidden:YES];
            self.choiceLabel4.textColor = RGB(112, 112, 112);
            [self.choiceLineView4 setHidden:YES];
            
            self.choiceLabel10.textColor = RGB(112, 112, 112);
            [self.choiceLineView10 setHidden:YES];
            self.choiceLabel11.textColor = RGB(112, 112, 112);
            [self.choiceLineView11 setHidden:YES];
            self.choiceLabel12.textColor = RGB(229, 163, 163);
            [self.choiceLineView12 setHidden:NO];
            self.choiceLabel13.textColor = RGB(112, 112, 112);
            [self.choiceLineView13 setHidden:YES];
            self.choiceLabel14.textColor = RGB(112, 112, 112);
            [self.choiceLineView14 setHidden:YES];
            break;
        case 3://寄卖
            self.choiceLabel0.textColor = RGB(112, 112, 112);
            [self.choiceLineView0 setHidden:YES];
            self.choiceLabel1.textColor = RGB(112, 112, 112);
            [self.choiceLineView1 setHidden:YES];
            self.choiceLabel2.textColor = RGB(112, 112, 112);
            [self.choiceLineView2 setHidden:YES];
            self.choiceLabel3.textColor = RGB(229, 163, 163);
            [self.choiceLineView3 setHidden:NO];
            self.choiceLabel4.textColor = RGB(112, 112, 112);
            [self.choiceLineView4 setHidden:YES];
            
            self.choiceLabel10.textColor = RGB(112, 112, 112);
            [self.choiceLineView10 setHidden:YES];
            self.choiceLabel11.textColor = RGB(112, 112, 112);
            [self.choiceLineView11 setHidden:YES];
            self.choiceLabel12.textColor = RGB(112, 112, 112);
            [self.choiceLineView12 setHidden:YES];
            self.choiceLabel13.textColor = RGB(229, 163, 163);
            [self.choiceLineView13 setHidden:NO];
            self.choiceLabel14.textColor = RGB(112, 112, 112);
            [self.choiceLineView14 setHidden:YES];
            break;
        case 4://代购
            self.choiceLabel0.textColor = RGB(112, 112, 112);
            [self.choiceLineView0 setHidden:YES];
            self.choiceLabel1.textColor = RGB(112, 112, 112);
            [self.choiceLineView1 setHidden:YES];
            self.choiceLabel2.textColor = RGB(112, 112, 112);
            [self.choiceLineView2 setHidden:YES];
            self.choiceLabel3.textColor = RGB(112, 112, 112);
            [self.choiceLineView3 setHidden:YES];
            self.choiceLabel4.textColor = RGB(229, 163, 163);
            [self.choiceLineView4 setHidden:NO];
            
            self.choiceLabel10.textColor = RGB(112, 112, 112);
            [self.choiceLineView10 setHidden:YES];
            self.choiceLabel11.textColor = RGB(112, 112, 112);
            [self.choiceLineView11 setHidden:YES];
            self.choiceLabel12.textColor = RGB(112, 112, 112);
            [self.choiceLineView12 setHidden:YES];
            self.choiceLabel13.textColor = RGB(112, 112, 112);
            [self.choiceLineView13 setHidden:YES];
            self.choiceLabel14.textColor = RGB(229, 163, 163);
            [self.choiceLineView14 setHidden:NO];
            break;
            
        default:
            break;
    }

}

-(void)setListOtherHeadView{
    [self.view addSubview:self.listOtherHeadView];
    //初始按钮状态
    self.choiceLabel10.textColor = RGB(229, 163, 163);
    [self.choiceLineView10 setHidden:NO];
}


#pragma mark - 交易方法--代购
-(void)businessInAction:(UIButton *)btn{
    
}

//分类点击
- (IBAction)categoryAction:(id)sender {
    @synchronized (self) {
        HNCategoryViewController *categoryVC = [[HNCategoryViewController alloc]init];
        [self.navigationController pushViewController:categoryVC animated:YES];
    }
   
    
}

#pragma mark - 搜索方法
- (IBAction)searchAction:(id)sender {
    ClassSearchController *searchVC = [[ClassSearchController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

#pragma mark - 购物车
- (IBAction)enterShoppingCar:(id)sender {
    
}



#pragma mark - 初始化数据
-(void)getIntializeDate{
      NSString * loginName = [NSString stringWithFormat:@"%@",[AppConfig getUserInfoByKey:@"hnLoginName"]];
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithObjectsAndKeys:loginName,@"hnLoginName", nil];
    NSLog(@"==param:%@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_MyPicture parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取详情失败"];
            return;
        }
        NSLog(@"==dict:%@",data);
        NSString * code = [NSString stringWithFormat:@"%@",[data objectForKey:@"error"]];
        if ([code isEqualToString:@"0"]) {
            //获取所有图片数据数据
            NSDictionary *infoData = [data objectForKey:@"data"];
               NSLog(@"infoData = %@",infoData);
            
            NSArray *infoArr = [infoData  objectForKey:@"imgList"];
            NSLog(@"infoArr = %@",infoArr);
            
            //获取轮播图图片数据
            NSMutableArray *srrollPicArr = [[NSMutableArray alloc]init];
            //获取广告图片数据
            NSMutableArray *adsPicArr = [[NSMutableArray alloc]init];
            for (int i = 0; i < infoArr.count; i++) {
                NSString *type = [NSString stringWithFormat:@"%@",[infoArr[i] objectForKey:@"hnMainType"]];
                
                NSString *urlStr = [NSString stringWithFormat:@"%@%@",url_Base_no,[infoArr[i] objectForKey:@"hnImgUrl"]];
                
                if ([type isEqualToString:@"2"]) {
                    [srrollPicArr addObject:urlStr];
                }else if ([type isEqualToString:@"3"]){
                    adsPicArr[0] = urlStr;
                }else if ([type isEqualToString:@"4"]){
                    adsPicArr[1] = urlStr;
                }else if ([type isEqualToString:@"5"]){
                    adsPicArr[2] = urlStr;
                }else if ([type isEqualToString:@"6"]){
                    adsPicArr[3] = urlStr;
                }
            }
            
            _contentHeight = 0;
            
            NSLog(@"srrollPicArr = %@",srrollPicArr);
            NSLog(@"adsPicArr = %@",adsPicArr);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置广告栏
                [self setAdsVC:srrollPicArr];
                
                //设置分类
                [self setCategory];
                
                //设置商品
                //            [self setGoodsViewAndImages:adsPicArr];
                
                [self setGoodsListViewAndImages:adsPicArr];
                //设置列表
                [self setListView];
                //刷新列表数据
                [self getGoodsDate:@"0"];
                //添加ListOtherView
                [self setListOtherHeadView];
                [self.listOtherHeadView setHidden:YES];
            });
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"message"]];
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取详情失败"];
    }];

}


//上拉刷新
-(void)refreshHeadDataSource{
    NSString * loginName = [NSString stringWithFormat:@"%@",[AppConfig getUserInfoByKey:@"hnLoginName"]];
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithObjectsAndKeys:loginName,@"hnLoginName", nil];
    NSLog(@"==param:%@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_MyPicture parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取详情失败"];
            return;
        }
        NSLog(@"==dict:%@",data);
        NSString * code = [NSString stringWithFormat:@"%@",[data objectForKey:@"error"]];
        if ([code isEqualToString:@"0"]) {
            //获取所有图片数据数据
            NSDictionary *infoData = [data objectForKey:@"data"];
            NSLog(@"infoData = %@",infoData);
            
            NSArray *infoArr = [infoData  objectForKey:@"proList"];
            NSLog(@"infoArr = %@",infoArr);
            
            if ([code isEqualToString:@"0"]) {
                //获取所有图片数据数据
                NSDictionary *infoData = [data objectForKey:@"data"];
                NSLog(@"infoData = %@",infoData);
                
                NSArray *infoArr = [infoData  objectForKey:@"imgList"];
                NSLog(@"infoArr = %@",infoArr);
                
                //获取轮播图图片数据
                NSMutableArray *srrollPicArr = [[NSMutableArray alloc]init];
                //获取广告图片数据
                NSMutableArray *adsPicArr = [[NSMutableArray alloc]init];
                for (int i = 0; i < infoArr.count; i++) {
                    NSString *type = [NSString stringWithFormat:@"%@",[infoArr[i] objectForKey:@"hnMainType"]];
                    
                    NSString *urlStr = [NSString stringWithFormat:@"%@%@",url_Base_no,[infoArr[i] objectForKey:@"hnImgUrl"]];
                    
                    if ([type isEqualToString:@"2"]) {
                        [srrollPicArr addObject:urlStr];
                    }else if ([type isEqualToString:@"3"]){
                        adsPicArr[0] = urlStr;
                    }else if ([type isEqualToString:@"4"]){
                        adsPicArr[1] = urlStr;
                    }else if ([type isEqualToString:@"5"]){
                        adsPicArr[2] = urlStr;
                    }else if ([type isEqualToString:@"6"]){
                        adsPicArr[3] = urlStr;
                    }
                }
                
                _contentHeight = 0;
                
                NSLog(@"srrollPicArr = %@",srrollPicArr);
                NSLog(@"adsPicArr = %@",adsPicArr);
                
//                //设置商品
//                [self setGoodsViewAndImages:adsPicArr];
//                //设置列表
//                [self setListView];
//                //刷新列表数据
//                [self getGoodsDate:@"0"];
//                //添加ListOtherView
//                [self setListOtherHeadView];
//                [self.listOtherHeadView setHidden:YES];
                if([infoArr isEqual:[NSNull null]]){
                    
                }else if (infoArr!=nil && infoArr.count>0) {
                    self.arrayImages = srrollPicArr;
                    self.fourPicArr = adsPicArr;
//                    [_contentScrollView ]
                    [_contentScrollView.mj_header  endRefreshing];
                }
            }
            NSLog(@"----self.goodsDataSource = %@",self.goodsDataSource);
            //刷新界面
            [self createListViewUI];
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"message"]];
            [self showHint:info];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取详情失败"];
    }];

}

#pragma mark - 获取商品详情数据
-(void)getGoodsDate:(NSString *)type{
     _goodsPage = 0;
    NSString * loginName = [NSString stringWithFormat:@"%@",[AppConfig getUserInfoByKey:@"hnLoginName"]];
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithObjectsAndKeys:loginName,@"hnLoginName", nil];
    [param setObject:type forKey:@"type"];
    [param setObject:[NSString stringWithFormat:@"%ld",_goodsPage] forKey:@"page"];
    NSLog(@"==param:%@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_MyGoods parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取详情失败"];
            return;
        }
        NSLog(@"==dict:%@",data);
        NSString * code = [NSString stringWithFormat:@"%@",[data objectForKey:@"error"]];
        if ([code isEqualToString:@"0"]) {
            //获取所有图片数据数据
            NSDictionary *infoData = [data objectForKey:@"data"];
            NSLog(@"infoData = %@",infoData);
            
            NSArray *infoArr = [infoData  objectForKey:@"proList"];
            NSLog(@"infoArr = %@",infoArr);
            for (int i = 0; i < infoArr.count; i ++) {
                [self.goodsDataSource addObject:infoArr[i]];
            }
            NSLog(@"----self.goodsDataSource = %@",self.goodsDataSource);
            //刷新界面
            [self createListViewUI];
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"message"]];
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取详情失败"];
    }];
    
}

#pragma mark - 刷新商品详情数据
-(void)refreshGoodsDate{
//    _goodsPage ++;
    [self hideHud];
    [self showHudInView:self.view hint:nil];
//    _goodsPage ++ ;
    NSString * loginName = [NSString stringWithFormat:@"%@",[AppConfig getUserInfoByKey:@"hnLoginName"]];
    
    NSMutableDictionary * param = [NSMutableDictionary dictionaryWithObjectsAndKeys:loginName,@"hnLoginName", nil];
    
    [param setObject:[NSString stringWithFormat:@"%@",self.goodsType] forKey:@"type"];
    
    [param setObject:[NSString stringWithFormat:@"%ld",_goodsPage] forKey:@"page"];
    
    NSLog(@"==param:%@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_MyGoods parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取详情失败"];
            return;
        }
        NSLog(@"==dict:%@",data);
        NSString * code = [NSString stringWithFormat:@"%@",[data objectForKey:@"error"]];
        if ([code isEqualToString:@"0"]) {
            //获取所有图片数据数据
            NSDictionary *infoData = [data objectForKey:@"data"];
            NSLog(@"infoData = %@",infoData);
            
            NSArray *infoArr = [infoData  objectForKey:@"proList"];
            NSLog(@"infoArr = %@",infoArr);
            
            if ([code isEqualToString:@"0"]) {
                //获取所有图片数据数据
                NSDictionary *infoData = [data objectForKey:@"data"];
                NSLog(@"infoData = %@",infoData);
                
                NSArray *infoArr = [infoData  objectForKey:@"proList"];
                NSLog(@"infoArr = %@",infoArr);
                if([infoArr isEqual:[NSNull null]]){
                    
                }else if (infoArr!=nil && infoArr.count>0) {
                    for (int i = 0; i < infoArr.count; i ++) {
                        [self.goodsDataSource addObject:infoArr[i]];
                    }
                }else{
                    [_contentScrollView.mj_footer endRefreshingWithNoMoreData];
                }
            }
            NSLog(@"----self.goodsDataSource = %@",self.goodsDataSource);
            //刷新界面
            [self createListViewUI];
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"message"]];
            [self showHint:info];
        }
            
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取详情失败"];
    }];
    
}


#pragma mark - 轮播图展现
-(void)loadScrollViewPage:(NSInteger)page
{
    if (page >= self.arrayImages.count) {
        return;
    }
    
    UIImageView *adsImageView = [self.viewController objectAtIndex:page];
    if ((NSNull *)adsImageView == [NSNull null])
    {
        adsImageView = [[UIImageView alloc] init];
        [self.viewController replaceObjectAtIndex:page withObject:adsImageView];
    }
    
    if (adsImageView.superview == nil) {
        CGRect frame = self.scrollView.frame;
        frame.origin.x = CGRectGetWidth(frame) * page;
        frame.origin.y = 0;
        adsImageView.frame = frame;
        
        [self.scrollView addSubview:adsImageView];
        NSLog(@"%@",self.arrayImages);
        
        if (self.arrayImages) {
            
            [adsImageView sd_setImageWithURL:[NSURL URLWithString:self.arrayImages[page]] placeholderImage:[UIImage imageNamed:@"ad_0.png"]];
        }else{
          [adsImageView sd_setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"ad_0.png"]];
        }

        
    }
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = CGRectGetWidth(self.scrollView.frame);
    NSInteger page = floor((self.scrollView.contentOffset.x -pageWidth/2)/pageWidth) ;
    if (self.scrollView.contentOffset.x<0) {
        _pageControl.currentPage = self.arrayImages.count - 1;
    }else if(self.scrollView.contentOffset.x > (self.arrayImages.count - 1)*Kwidth){
     _pageControl.currentPage = 0;
    }else{
        _pageControl.currentPage = page + 1;
    }
    [self loadScrollViewPage:page];
//    [self loadScrollViewPage:page-1];
//    [self loadScrollViewPage:page];
//    [self loadScrollViewPage:page+1];
}

//- (IBAction)changePage:(id)sender
//{
//    NSInteger page = self.pageControl.currentPage;
//
//    [self loadScrollViewPage:page - 1];
//    [self loadScrollViewPage:page];
//    [self loadScrollViewPage:page + 1];
    
//    CGRect bounds = self.scrollView.bounds;
//    bounds.origin.x = Kwidth * page - 1;
//    bounds.origin.y = 0;
//    [self.scrollView scrollRectToVisible:bounds animated:YES];
//}


//NSTimer
-(void)scrollPages{
//    ++self.pageControl.currentPage;
    CGFloat pageWidth = CGRectGetWidth(self.scrollView.frame);
    if (isFromStart) {
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
        _pageControl.currentPage = 0;
    }
    else
    {
        _pageControl.currentPage ++;
        
        [self.scrollView setContentOffset:CGPointMake(pageWidth*_pageControl.currentPage, self.scrollView.bounds.origin.y)];
      
        [self loadScrollViewPage:_pageControl.currentPage];
    }
    if (_pageControl.currentPage == _pageControl.numberOfPages - 1) {
        isFromStart = YES;
    }
    else
    {
        isFromStart = NO;
    }
}

#pragma mark - 轮播图 UIPageControl
-(void)chagePage:(UIPageControl *)pageControl
{
//    _pageControl.currentPage
}


#pragma mark - scrollViewDelegate
// contentScrollView 已经滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offset = _noListNormalH - 58 - 36 - 36;
    CGFloat contentScrolloffectY = _contentScrollView.contentOffset.y;
    if (_noListNormalH != 0) {
        if (contentScrolloffectY >= offset) {
           self.srarchView.alpha = 1;
            [self.listHeadView setHidden:YES];
            [self.listOtherHeadView setHidden:NO];
            [self.searchBackground setBackgroundColor:RGB(244, 244, 244)];
        }else{
            self.srarchView.alpha = contentScrolloffectY/offset;
            [self.listHeadView setHidden:NO];
            [self.listOtherHeadView setHidden:YES];
            [self.searchBackground setBackgroundColor:RGB(255, 255, 255)];
        }
    }
    
//    NSLog(@"scrollViewDidScroll");
}

// contentScrollView 开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//    NSLog(@"scrollViewWillBeginDragging");
}

// contentScrollView 结束拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
//    NSLog(@"scrollViewDidEndDragging");
}


//判断是否为空
-(BOOL)isNull:(NSString *)inputStr{
    if (inputStr.length == 0 || [inputStr isEqualToString:@"<null>"]  || [inputStr isEqualToString:@"null"]  || [inputStr isEqualToString:@"(null)"]) {
        return 1;
    }else{
        return 0;
    }
}



@end
